package com.bingxue.edu.parent.service;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.management.shop.model.query.ParentOrderQuery;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.management.shop.service.RefundService;
import com.bingxue.edu.parent.model.query.StudentRefundQuery;
import com.bingxue.edu.parent.model.resp.ParentOrderResp;
import com.bingxue.edu.parent.model.resp.StudentRefundResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.shop.model.entity.table.OrderTableDef.ORDER;
import static com.bingxue.edu.management.shop.model.entity.table.RefundTableDef.REFUND;

/**
 * 家长订单业务逻辑
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StudentOrderService {

    private final OrderService orderService;
    private final RefundService refundService;

    public Page<ParentOrderResp> getOrderPage(ParentOrderQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(ORDER.DEFAULT_COLUMNS)
                .where(ORDER.STUDENT_ID.eq(query.getStudentId()))
                .and(ORDER.STATUS.in(query.getStatus()))
                .orderBy(ORDER.ID.desc());
        return orderService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, ParentOrderResp.class);
    }

    public List<StudentRefundResp> getRefundPage(StudentRefundQuery query, PageQuery pageQuery) {
        // 根据学生id查询退费订单
        return refundService.queryChain()
                .where(REFUND.STUDENT_ID.eq(query.getStudentId()))
                .where(REFUND.REFUND_OFF.eq(query.getRefundOff()))
                .orderBy(REFUND.ID.desc())
                .withRelations()
                .listAs(StudentRefundResp.class);
    }
}
